package com.hznu.stmanage.web.controller;

import com.hznu.stmanage.beans.LoginInfo;
import com.hznu.stmanage.entity.Manager;
import com.hznu.stmanage.entity.Student;
import com.hznu.stmanage.entity.Teacher;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by J_Vino on 16/3/15.
 */
@Controller
@RequestMapping("/")
public class LoginController {

    public ModelAndView view = new ModelAndView();

    Log log = LogFactory.getLog(LoginController.class);

    @RequestMapping("login")
    public ModelAndView login(LoginInfo loginInfo, HttpServletRequest request, HttpServletResponse response){

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();

        if(subject.isAuthenticated()){//已登录用户
            Object object = session.getAttribute("currentUser");
            if(Student.class.isInstance(object)){
                view.setViewName("");
                return view;
            }
            if(Teacher.class.isInstance(object)){
                Teacher teacher = (Teacher) object;
                switch (teacher.getLevel()){
                    case 1:
                        view.setViewName("");//普通教师
                        break;
                    case 2:
                        view.setViewName("");//系主任
                        break;
                    default:
                        log.warn("warn-->> /login-->>isAuth-->>Teacher");
                        break;
                }
                return view;
            }
            if(Manager.class.isInstance(object)){
                Manager manager = (Manager) object;
                switch (manager.getCategory()){
                    case 1:
                        view.setViewName("");//系统管理员
                        break;
                    case 2:
                        view.setViewName("");//实践管理员
                        break;
                    case 3:
                        view.setViewName("");//就业管理员
                        break;
                    case 4:
                        view.setViewName("");//企业管理员
                        break;
                    default:
                        log.warn("warn-->> /login-->isAuth-->>Manager");
                        break;
                }
                return view;
            }
        }

        if(request.getMethod().equalsIgnoreCase("get")){//请求方式为get
            view.setViewName("login");
            return view;
        }
        //if (! ControllerUtils.userLogin(subject,request,session)){
        //    view.setViewName("");//跳转到登录失败页面
        //    return view;
        //}


        return view;
    }
}
